package LeetCode;

public class LC_283_MoveZeroes {

    public static void main(String[] args) {

    }

    static class Solution {
        public void moveZeroes(int[] nums) {
            int zeroIndex = 0;
            int nonZeroIndex = 0;
            int size = nums.length;

            while (zeroIndex < size && nonZeroIndex < size) {
                while (zeroIndex < size && nums[zeroIndex] != 0) zeroIndex++;
                while (nonZeroIndex < size && (nums[nonZeroIndex] == 0 || nonZeroIndex < zeroIndex)) nonZeroIndex++;
                if (zeroIndex < size && nonZeroIndex < size) {
                    nums[zeroIndex++] = nums[nonZeroIndex];
                    nums[nonZeroIndex++] = 0;
                }
            }
        }

    }

    static class Solution_2 {
        public void moveZeroes(int[] nums) {
            if (nums != null) {
                int length = nums.length;
                for (int i = 0, j = 0; i < length; i++) {
                    if (nums[i] != 0) {
                        if (i != j) {
                            nums[j] = nums[i];
                            nums[i] = 0;
                        }
                        j++;
                    }
                }
            }
        }

    }
}